package com.hyt.it.ogt.controller.pj;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.it.ogt.controller.base.BaseController;
import com.hyt.it.ogt.pj.common.Constants;
import com.hyt.it.ogt.pj.common.ResponseCode;
import com.hyt.it.ogt.pj.model.entity.Config;
import com.hyt.it.ogt.pj.service.IConfigService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;

/**
 * <p>
 * 评卷参数表 前端控制器
 * </p>
 *
 * @author qiaozj
 * @since 2020-07-09
 */
@ApiSort(value = 8)
@Api(tags = "8.评卷参数接口", value = "评卷参数接口")
@RestController
@RequestMapping("/config")
public class ConfigController extends BaseController {

    @Autowired
    private IConfigService configService;

    @ApiOperationSort(value = 1)
    @ApiOperation(value = "8.1.1根据id获取参数", notes = Constants.SWAGGER_METHOD_NOTES)
    @GetMapping("query/{id}")
    public Object query(@PathVariable("id") String id){
        Config config = configService.getById(id);
        if(config != null){
            return ApiResponse.builder().code(ResponseCode.SUCCESS).data(config).build();
        }else{
            return ApiResponse.builder().code(ResponseCode.ERROR).build();
        }
    }

    @ApiOperationSort(value = 2)
    @ApiOperation(value = "8.1.2获取所有评卷参数", notes= Constants.SWAGGER_METHOD_NOTES)
    @GetMapping("findProjectConfigs")
    public Object findProjectConfigs(@RequestParam String projectId){
        List<Config> configs = configService.findProjectConfigs(projectId);
        if(configs != null){
            return ApiResponse.builder().code(025201).build();
        }else{
            return ApiResponse.builder().code(ResponseCode.SUCCESS).data(configs).build();
        }
    }

    @ApiOperationSort(value = 10)
    @ApiOperation(value = "8.2.1添加评卷参数", notes= Constants.SWAGGER_METHOD_NOTES)
    @GetMapping("add")
    public Object add(@RequestBody Config config){
        Boolean flag = configService.save(config);
        if(flag){
            return ApiResponse.builder().code(ResponseCode.SUCCESS).build();
        }else{
            return ApiResponse.builder().code(ResponseCode.ERROR).build();
        }
    }

    @ApiOperationSort(value = 11)
    @ApiOperation(value = "8.2.2初始化评卷参数", notes = Constants.SWAGGER_METHOD_NOTES)
    @GetMapping("init")
    public Object init(@RequestParam String projectId) throws Exception {
        Integer code = configService.initMarkParam(projectId);
        return ApiResponse.builder().code(code).build();
    }

    @ApiOperationSort(value = 20)
    @ApiOperation(value = "8.3.1更新参数", notes = Constants.SWAGGER_METHOD_NOTES)
    @PutMapping("update")
    public Object update(@RequestBody Config config){
        boolean flag = configService.updateById(config);
        if(flag){
            return ApiResponse.builder().code(ResponseCode.SUCCESS).build();
        }else{
            return ApiResponse.builder().code(ResponseCode.ERROR).build();
        }
    }

    @ApiOperationSort(value = 30)
    @ApiOperation(value = "8.4.1根据id删除参数", notes = Constants.SWAGGER_METHOD_NOTES)
    @DeleteMapping("delete/{id}")
    public Object delete(@PathVariable("id") String id){
        boolean flag = configService.removeById(id);
        if(flag){
            return ApiResponse.builder().code(ResponseCode.SUCCESS).build();
        }else{
            return ApiResponse.builder().code(ResponseCode.ERROR).build();
        }
    }

    @ApiOperationSort(value = 31)
    @ApiOperation(value = "8.4.2根据项目id删除评卷参数", notes = Constants.SWAGGER_METHOD_NOTES)
    @DeleteMapping("deleteByProjectId")
    public Object deleteByProjectId(@RequestParam("projectId") String projectId){
        Integer code = configService.deleteByProjectId(projectId);
        return ApiResponse.builder().code(code).build();
    }
}
